import axios from 'axios'
// create an axios instance
const service = axios.create({
    baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
    headers: {
        'Content-Type': 'application/json'
    },
    timeout: 0 // request timeout
})


service.interceptors.request.use(
    config => {
        config.headers = {
            ...config.headers
        }
        return config
    },
    error => {
        return Promise.reject(error)
    }
)

// response interceptor
service.interceptors.response.use(

    async response => {
        const res = response.data
        if (res.errcode === 0) {
            return res.data ? res.data : true
        } else {
            alert(res.errmsg)
            return false
        }
    },
    error => {
        console.log('err' + error) // for debug
        alert(error)
        return Promise.reject(error)
    }
)

export default service